"""
用户分群相关接口
"""
from fastapi import APIRouter
from schemas import schema_group
from models import model_group

router_group = APIRouter(prefix='/group', tags=['分群管理'])


@router_group.get('/', summary='查询所有分群', tags=['分群列表'])
async def get_group_list(page: int, limit: int):
    """分页查询列表"""
    item = await model_group.Group.filter().offset(page * limit).limit(limit)
    return item


@router_group.post('/', summary='新增分群', tags=['分群列表'])
async def add_group_detail(item: schema_group.Group):
    item = await model_group.Group.create(group_name=item.group_name,
                                          group_desc=item.group_desc,
                                          group_type=item.group_type,
                                          configs=item.configs)
    return item


@router_group.get('/{group_id}', summary='获取分群详细信息', tags=['分群列表'])
async def get_group_detail(group_id: int):
    item = await model_group.Group.get(group_id=group_id)
    return item


@router_group.put('/{group_id}', summary='修改分群详细信息', tags=['分群列表'])
async def update_group_detail(item: schema_group.Group):
    item = await model_group.Group.filter(group_id=item.group_id).update(group_name=item.group_name,
                                                                         group_desc=item.group_desc,
                                                                         group_type=item.group_type,
                                                                         comfig=item.config)
    return item


# @router_group.put('/delete/{group_id}', summary='修改分群启用分群状态', tags=['分群列表'])
# async def update_group_detail(group_id: int):
#     item = await model_group.Group.filter(group_id=group_id).update(is_deleted=1)
#     return item


@router_group.delete('/{group_id}', summary='删除分群详细信息', tags=['分群列表'])
async def delete_group_detail(group_id: int):
    item = await model_group.Group.filter(group_id=group_id).update(is_deleted=1)
    return item


@router_group.get('/type', summary='查询所有分群类型', tags=['分群类型'])
async def get_group_type_list(page: int, limit: int):
    """分页查询列表"""
    item = await model_group.Group.filter().offset(page * limit).limit(limit)
    return item


@router_group.post('/type', summary='新增分群类型', tags=['分群类型'])
async def add_group_type_detail(item: schema_group.Group):
    item = await model_group.Group.create(group_type=item.group_type,
                                          group_type_key=item.group_type_key,
                                          group_type_name=item.group_type_name)
    return item


@router_group.get('/type/{group_type}', summary='获取分群类型详细信息', tags=['分群类型'])
async def get_group_type_detail(group_type: int):
    item = await model_group.GroupType.get(group_type=group_type)
    return item


@router_group.put('/type/{group_type}', summary='修改分群类型详细信息', tags=['分群类型'])
async def update_group_type_detail(item: schema_group.Group):
    item = await model_group.GroupType.filter(group_type=item.group_type).update(group_type=item.group_type,
                                                                                 group_type_key=item.group_type_key,
                                                                                 group_type_name=item.group_type_name)
    return item


@router_group.delete('/type/{group_type}', summary='删除分群类型详细信息', tags=['分群类型'])
async def delete_group_type_detail(group_type: int):
    item = await model_group.GroupType.filter(group_type=group_type).delete()
    return item
